package com.situ.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.admin.bean.APIResult;
import com.situ.admin.bean.Goods;
import com.situ.admin.service.GoodsService;

@RestController
@RequestMapping("/api/goods")
@CrossOrigin(origins = { "*" })
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	@PostMapping
	public APIResult add(@RequestBody Goods goods) {
		goods = goodsService.add(goods);

		return APIResult.created(goods);
	}

	@DeleteMapping
	public APIResult remove(Integer id) {
		Goods goods = goodsService.remove(id);

		if (goods != null) {
			return APIResult.ok(goods);
		} else {
			return APIResult.notFound("要删除的资源不存在");
		}

	}

	@PutMapping
	public APIResult edit(@RequestBody Goods goods) {
		goods = goodsService.edit(goods);
		if (goods != null) {
			return APIResult.created(goods);
		} else {
			return APIResult.notFound("要修改的资源不存在");
		}

	}

	@GetMapping
	public APIResult get(Integer page, Integer limit, Goods goods) {
		PageInfo pageInfo = goodsService.getByPage(page, limit, goods);
		if (pageInfo == null || pageInfo.getTotal() == 0) {
			return APIResult.notFound("没有查询到任何数据");
		} else {
			return APIResult.ok(pageInfo);
		}
	}
}
